package com.marktotop.controller.business;


import com.marktotop.entity.business.ReservationRecord;
import com.marktotop.entity.business.Vaccinator;
import com.marktotop.error.SystemException;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IReservationRecordService;
import com.marktotop.service.IVaccinationSiteService;
import com.marktotop.service.IVaccinatorService;
import com.marktotop.vo.business.ReservationRecordVO;
import com.marktotop.dto.SiteAndVaccDTO;
import com.marktotop.vo.business.SiteAndVaccsVO;
import com.marktotop.vo.business.VaccineInSiteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器 - 预约记录
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Api(tags = "业务模块-疫苗预约接口")
@RestController
@RequestMapping("/business/reservate")
public class ReservationRecordController {

    @Autowired
    private IReservationRecordService reservationService;

    @Autowired
    private IVaccinationSiteService siteService;

    @Autowired
    private IVaccinatorService vaccinatorService;


    /**
     * 分页展示所有预约记录列表，并进行模糊查询
     *
     * @param pageNum
     * @param pageSize
     * @param reservationRecordVO
     * @return
     */
    @ApiOperation(value = "预约记录", notes = "疫苗预约记录列表,并进行模糊查询")
    @RequestMapping(value = "/findList",method = RequestMethod.GET)
    public ResultBean findReservationRecordsList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize") Integer pageSize,
                                                ReservationRecordVO reservationRecordVO ) throws ParseException {
        return reservationService.findReservationRecordsList(pageNum,pageSize,reservationRecordVO);
    }


    /**
     * 查询所有接种人信息，供预约时使用
     *
     * @return
     */
    @ApiOperation(value = "所有接种人信息", notes = "返回所有接种人信息，供预约时使用")
    @RequestMapping(value = "/findVaccinators",method = RequestMethod.GET)
    public ResultBean findVaccinatorsList(){
        List<Vaccinator> allVaccinators = vaccinatorService.findAllVaccinatorInfo();
        if (allVaccinators.isEmpty()){
            return ResultBean.error().message("获取所有接种人信息失败!");
        }
        return ResultBean.ok().data("vaccinators",allVaccinators).message("获取所有接种人信息成功!");
    }


    /**
     * 查询某个接种点下，某个疫苗信息
     *
     * @param siteAndVaccDTO
     * @return
     */
    @ApiOperation(value = "查询接种点某个疫苗(C)", notes = "查询某个接种点下，某个疫苗信息")
    @RequestMapping(value = "/findVaccine",method = RequestMethod.GET)
    public ResultBean findVaccineRecordsList(SiteAndVaccDTO siteAndVaccDTO){
        VaccineInSiteVO vaccInSite = reservationService.findVaccAndSite(siteAndVaccDTO);
        if (null == vaccInSite) {
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("siteVaccInfo",vaccInSite)
                              .message("获取当前接种点的该疫苗信息成功！");
    }


    /**
     * 预约某个疫苗
     *
     * @param record
     * @return
     * @throws SystemException
     */
    @ApiOperation(value = "预约某个疫苗(C)", notes = "预约某个接种点的疫苗，库存-1")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @RequiresPermissions({"reservate:add"})
    public ResultBean addReservationRecords(@RequestBody ReservationRecord record) throws SystemException {
        if(!reservationService.addReservationRecords(record)){
            return ResultBean.error().message("预约疫苗信息失败！");
        }
        return ResultBean.ok().message("预约疫苗信息成功！");
    }


    /**
     * 获取单个预约信息
     *
     * @param id
     * @return
     * @throws SystemException
     */
    @ApiOperation(value = "获取单个预约信息(C)", notes = "获取某条预约信息")
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    @RequiresPermissions({"reservate:edit"})
    public ResultBean getOneReservationRecords(@PathVariable Long id) throws SystemException {
        ReservationRecordVO oneRecord = reservationService.getOneRecord(id);
        if(oneRecord == null){
            return ResultBean.error().message("查询疫苗信息失败！");
        }
        return ResultBean.ok().data("result",oneRecord).message("查询疫苗信息成功！");
    }


    /**
     * 受理单个预约信息（修改预约状态）
     *      *
     * @param id
     * @return
     * @throws SystemException
     */
    @ApiOperation(value = "受理预约信息", notes = "受理单个预约信息")
    @RequestMapping(value = "/accept/{id}",method = RequestMethod.GET)
    @RequiresPermissions({"reservate:accept"})
    public ResultBean reservationRecordAccept (@PathVariable Long id) throws SystemException {
        if(!reservationService.acceptRecord(id)){
            return ResultBean.error().message("预约疫苗信息受理失败！");
        }
        return ResultBean.ok().message("预约疫苗信息受理成功！");
    }


    /**
     * 查询接种地的所有疫苗信息
     *
     * @param id
     * @return
     * @throws SystemException
     */
    @ApiOperation(value = "查询接种地所有疫苗(C)", notes = "查询某接种地的所有疫苗信息")
    @RequestMapping(value = "/getVacc/{id}",method = RequestMethod.GET)
    public ResultBean getSiteVaccineList(@PathVariable Long id) {
        SiteAndVaccsVO siteVaccineList = siteService.getSiteVaccineList(id);
        if(siteVaccineList == null){
            return ResultBean.error().message("查询疫苗信息失败！");
        }
        return ResultBean.ok().data("allVaccinesInSite",siteVaccineList).message("查询疫苗信息成功！");
    }


    /**
     * 取消预约信息
     *
     * @param id
     * @return
     * @throws SystemException
     */
    @ApiOperation(value = "取消预约", notes = "删除预约信息，库存+1")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    @RequiresPermissions({"reservate:cancel"})
    public ResultBean cancelReservationRecords(@PathVariable Long id) throws SystemException {
        if(!reservationService.cancelReservation(id)){
            return ResultBean.error().message("取消预约疫苗信息失败！");
        }
        return ResultBean.ok().message("取消预约疫苗信息成功！");
    }
}
